package com.renbang.action.devoloper;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.renbang.dao.sqlsession.DBUtils;
import com.renbang.model.common.AjaxResult;
import com.renbang.model.common.Page;
import com.renbang.model.common.PageData;
import com.renbang.model.finance.FinanceOrg;
import com.renbang.service.finance.FinanceOrgService;
import com.renbang.utils.excel.ExcelImport;

@Controller
@RequestMapping(value = "developer/DownAndUpLoad")
public class DownAndUpLoad {
	@Autowired
	SqlSessionTemplate sqlSession;
	@Autowired
	FinanceOrgService financeOrgService;
	@RequestMapping(value = "/importData")
	public @ResponseBody AjaxResult importChance(HttpServletResponse response,
			HttpServletRequest request,
			@RequestParam(value = "file", required = false) MultipartFile file)
			throws Exception {
		response.setContentType("text/html;charset=UTF-8");
		String fileName = file.getOriginalFilename();
		String dirPath = request.getSession().getServletContext()
				.getRealPath("/")
				+ "fileupload";
		File fileDir = new File(dirPath);
		if (!fileDir.exists()||!fileDir.isDirectory()) {
			if (fileDir.mkdirs()) {
			}
		}
		File saveFile = new File(dirPath + "/" + fileName);
		try {
			file.transferTo(saveFile);
			
			List<TreeMap<String, Object>> list = ExcelImport
					.readExcelAndCsv(saveFile);
			List<FinanceOrg> financeOrgs = new ArrayList<FinanceOrg>();
			int i=0;
		/*	for (TreeMap<String, Object> treeMap : list) {
				if(i>1){
				FinanceOrg financeOrg = new FinanceOrg();
				System.out.println("开始");
				String orgName = (String)treeMap.get("td0");
				System.out.println(orgName);
				financeOrg.setOrgName(orgName);
				String area  =  (String)treeMap.get("td1");
				System.out.println(area);
				financeOrg.setArea(area);
				String zone  = (String)treeMap.get("td2");
				System.out.println(zone);
				financeOrg.setZone(zone);
				financeOrg.setOrgType("证券公司");
				financeOrgService.add(financeOrg);
				financeOrgs.add(financeOrg);
				}
				i++;
			}*/
			//批量插入
//			System.out.println(financeOrgs.size());
//			financeOrgService.batchAdd(financeOrgs);//在mybatis里面批量插入sqlserver最多一次110条
			//System.out.println(list);
			System.out.println(list.get(0).size());
			DBUtils.createTemp(list.get(0).size());
			DBUtils.insertTemp(list, list.get(0).size());
			return new AjaxResult(AjaxResult.STATUS_OK, "加载成功","1",list,list.size());
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	
	@RequestMapping(value = "/showImpData")
	public @ResponseBody AjaxResult showImpData(HttpServletRequest request,Page page,String colnum){
		List list = null;
		int count = 0;
		try {
			list = DBUtils.getList("mytemp", "id,"+colnum.substring(0, colnum.length()-1), "", page.getOffset(), page.getLimit());
			count = DBUtils.getList("mytemp", "id", "").size();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new AjaxResult(AjaxResult.STATUS_OK,page.getCurrpage()+"", "数据加载成功", list, count);
	}
	
	
	@RequestMapping(value = "/downLoadFile")
	public void downLoad(HttpServletResponse response,
			HttpServletRequest request) throws Exception {
		response.setContentType("application/x-download");// 设置为下载application/x-download
		String basePath = request.getSession().getServletContext()
				.getRealPath("WEB-INF");
		System.out.println(basePath);
		String filedownload = basePath + "/views/upload6.csv";
		System.out.println(filedownload);
		String filedisplay = "表格文件.csv";
		String filenamedisplay = URLEncoder.encode(filedisplay, "UTF-8");
		response.addHeader("Content-Disposition", "attachment;filename="
				+ filenamedisplay);
		OutputStream out = response.getOutputStream();
		FileInputStream io = new FileInputStream(filedownload);
		int i = 0;
		byte[] b = new byte[1024];
		while ((i = io.read(b)) > 0) {
			out.write(b, 0, i);
		}
		// 6.关流
		io.close();
		out.close();
		System.out.println("下载成功");
		/*
		 * try { RequestDispatcher dis =
		 * request.getSession().getServletContext()
		 * .getRequestDispatcher(filedownload); if(dis!= null) {
		 * dis.forward(request,response); } response.flushBuffer(); }
		 * catch(Exception e) { e.printStackTrace(); } finally { }
		 */
	}
}
